<?php
/**
 +--------------------------------
 * ProductAction.class.php （产品操作模块）
 +--------------------------------
 *
 */
class ProductAction extends BaseAction {
	protected $model_name, $model_id, $newpro, $attrcate, $attribute, $proattr, $brand, $procate, $protocate, $prodes, $catetemp, $product;
	function _initialize() {
		parent::_initialize ();
		self::_setmod ();
		import ( 'Com.Product' );
		import ( 'Com.Newpro' );
		$this->dao = D ( 'Product' );
		$this->attribute = M ( 'Attribute' );
		$this->attrcate = M ( 'CategoryAttribute' );
		$this->proattr = M ( 'ProductAttribute' );
		$this->brand = M ( 'ProductBrand' );
		$this->procate = M ( 'ProductCategoryTag' );
		$this->protocate = M ( 'ProductToCategoryTag' );
		$this->prodes = M ( 'ProductDescription' );
		$this->product = new Product ();
		$this->newpro = new Newpro();
	}
	
	
	//更新商品分类 -- 暂时
	function updatecat(){
		if($this->username !='adminwei'){
			$this->_ajm ( 300, '权限不足!', 0 );
			exit;
		}
			
		
		$dao = M("product");
		$dao_cat = M("product_to_category_tag");
		$list = $dao->field('id')->where('mian_cat=3')->order('id desc')->select();
		$i = 0;
		foreach ($list as $vo){
			$data = array();
			$res = $dao_cat->where('product_id='.$vo['id'])->find();
			if(empty($res)){
				$data['product_id'] = $vo['id'];
				$data['category_tag_id'] = 3;
				$data['type'] = 1;
				$dao_cat->add($data);
				$i++;
			}
		}
		
		print_r('总共更新了'.$i.'款商品！');
		
	}
	
	/**
	 * 初始化模块信息
	 */
	function _setmod($model_name, $model_id) {
		$this->model_name = $model_name;
		$this->model_id = $model_id;
	}
	
	function index() {
		$model = M('product');
		$where = array();
		if ( $_REQUEST['mname'] ){ $where['mname'] = $_REQUEST['mname'];}
		else { $where['_string'] = 'mname is null';}
		if ( $_REQUEST['mian_cat'] ) $where['mian_cat'] = $_REQUEST['mian_cat'];
		$where=parent::create_where($where);
		
		$count = $model->where($where)->count('id');
		
		$page = new Page($count,$this->listrows);
		$offset = $page -> firstRow;
		$length = $page -> listRows;
		$limit = "$offset , $length";
		//dump($where);
		$proinfo = $model->where($where)->limit($limit)->select();
// 		echo $model->getLastSql();
		foreach ($proinfo as $key=>$vo){
			$proinfo[$key]['attr'] = $this->product->protoattr($vo['id']);
			$proinfo[$key]['cat'] = $this->product->procat($vo['id']);
		}
		$m = M('ProductBrand');
		$brand = $m->field('id,name')->cache(true,600)->select();
		$brand = field2key($brand,'id');
		$this->assign('brand',$brand);
		$this->assign("totalCount",$count);
		$this->assign("sfield_name", $sfield_name);
		$this->assign('list',$proinfo);
		$this->assign('brand',$brand);
		$this->assign('mname',$_REQUEST['mname']);
		$this->display();
	}
	
	
	// 产品详情
	function profile() {
		$product = $this->dao->relation ( true )->find ( $_REQUEST ['id'] );
		$this->assign ( 'product', $product );
		
		$map ['id'] = $_REQUEST ['id'];
		$model = D ( 'ProductPriceListView' );
		$product = $model->where ( $map )->select ();
		$this->assign ( 'price_list', $product );
		
		$this->display ();
	}
	
	/**
	 * 在线编辑图片上传
	 */
	function upload() {
		$config = array ();
		$imagepath = parent::_upload ( $config );
		$msg = "{'url':'" . $imagepath . "'}";
		$err = '';
		echo "{'err':'" . jsonString ( $err ) . "','msg':" . $msg . "}";
	}
	function img_list() {
		$dao = M ( "product_image" );
		parent::dwz_list ( array (
				'product_id' => $_GET ['id'] 
		), $dao );
	}
	function set_param() {
		$dao = M ( $_REQUEST ['model'] );
		$dao->where ( array (
				'id' => $_REQUEST ['id'] 
		) )->setField ( $_REQUEST ['type'], $_REQUEST ['value'] );
		echo $dao->getLastSql ();
	}
	
	/**
	 * 产品编辑表单页
	 * (non-PHPdoc)
	 * 
	 * @see BaseAction::edit()
	 */
	function edit() {
		$cid = $_REQUEST ['cid'];
		$proid = $_REQUEST ['id'];
		$brand = $this->brand->field ( 'id,name' )->where ( array (
				'status' => '1' 
		) )->select ();
		$description = $this->prodes->where ( array (
				'product_id' => $proid 
		) )->find ();
		$product = M ( 'Product' );
		$baseinfo = $product->where ( array (
				'id' => $proid 
		) )->find ();
		$procateinfo = $this->protocate->where ( array (
				'product_id' => $proid 
		) )->find ();
		// dump($baseinfo);
		$categorys = $this->procate->field ( array (
				'id,parent_id,name' 
		) )->where ( array (
				'type' => '1' 
		) )->select ();
		$tag = $this->procate->field ( array (
				'id,name' 
		) )->where ( array (
				'type' => '2' 
		) )->select ();
		$cate = self::category ( $categorys );
		$sty = array (
				'0' => '&nbsp;&nbsp;&nbsp;',
				'1' => '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|--',
				'2' => '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;&nbsp;&nbsp;&nbsp;|--',
				'3' => '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;&nbsp;&nbsp;&nbsp;|--',
				'4' => '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;&nbsp;&nbsp;&nbsp;|--' 
		);
		
		// dump($cate['0']['chiled']);
		$this->assign ( 'tag', $tag );
		$this->assign ( 'style', $sty );
		$this->assign ( 'cateinfo', $procateinfo );
		$this->assign ( 'cate', $cate );
		$this->assign ( 'prodes', $description );
		$this->assign ( 'baseinfo', $baseinfo );
		$this->assign ( 'brand', $brand );
		$this->assign ( 'cid', $cid );
		$this->assign ( 'proid', $proid );
		$this->display ();
	}
	
	function relevance(){
		$to = explode(',', $_REQUEST['org3_id']);
		$data = array();
		$data['from'] = $_REQUEST['pid'];
		$data['status'] = 1;
		$model = M('ProductRelated');
		$reid = "";
		foreach ($to  as $k => $v){
			$data['to'] = $v;
			$reid = $model->add($data);
		}
		if ($reid === FALSE) {
			return $this->_ajm ( 300, '添加失败，请重试', 1 );
		}else {
			return $this->_ajm ( 200, '添加成功！', 0 );
		}
	}
	
	/**
	 * 编辑关联产品
	 */
	function relevedit(){
		$id = $_REQUEST['id'];
		$proinfo = $this->newpro->protobaseinfo($id);
		$relev = $this->newpro->relevance($id);
		$relev = $this->newpro->protobaseinfo($relev);
		$proinfo = array_shift($proinfo);
		$this->assign('relev',$proinfo);
		$this->assign('torelev',$relev);
		$this->display();
	}
	
	/**
	 * 删除关联产品
	 * @return JSON
	 */
	function delrelev(){
		$model = M('ProductRelated');
		$where = array();
		$where['from'] = $_REQUEST['id'];
		$where['to'] = $_REQUEST['pid'];
		$reid = $model->where($where)->delete();
		if ($reid === FALSE) {
			return $this->_ajm ( 300, '删除失败，请重试', 0 );
		}else {
			return $this->_ajm ( 200, '删除成功！', 0 );
		}
	}
	
	function lookpro(){
		$keyword = $_REQUEST['keyword'];
		if($keyword){
			$model = M('Product');
			$where = array();
			$where['name'] = array('like',"%$keyword%");
			$count = $model->where($where)->count();
			
			$page = new Page($count,50);
			$offset = $page -> firstRow;
			$length = $page -> listRows;
			$limit = "$offset , $length";
			
			$info = $model->field('id,mian_cat,name,status')->where($where)->limit($limit)->order('id DESC')->select();
			$this->assign('keyword',$keyword);
			$this->assign('info',$info);
		}
		$this->display();
	}
	
	/**
	 * 处理产品分类
	 * 
	 * @param array $_categorys
	 *        	产品分类
	 * @return array
	 */
	function category($_categorys) {
		$temp = array ();
		foreach ( $_categorys as $k => $v ) {
			$this->catetemp = array ();
			if ($v ['parent_id'] == '0') {
				$temp [$k] = $v;
				$temp [$k] ['chiled'] = $this->catlist ( $_categorys, $v ['id'] );
			}
		}
		return $temp;
	}
	
	/**
	 * 返回分类层次
	 * 
	 * @param array $_categorys
	 *        	产品分类
	 * @param string $_id
	 *        	产品ID
	 * @param string $_level
	 *        	结构层次
	 * @return array
	 */
	function catlist($_categorys, $_id, $_level = 0) {
		foreach ( $_categorys as $k => $v ) {
			if ($_id == $v ['parent_id']) {
				$this->catetemp [$v ['id']] = $v;
				$this->catetemp [$v ['id']] ['level'] = $_level;
				$this->catlist ( $_categorys, $v ['id'], $_level + 1 );
			}
		}
		return $this->catetemp;
	}
	/**
	 * 修改产品基本信息
	 */
	function baseinfo() {
		$proid = $_REQUEST ['proid'];
		$reid = $this->protocate->where ( array (
				'product_id' => $_REQUEST ['proid'] 
		) )->delete ();
		if ($reid) {
			$data = array ();
			foreach ( $_REQUEST ['attachedcate'] as $value ) {
				$tmp = array ();
				$tmp ['type'] = $_REQUEST ['attachedtype'];
				$tmp ['product_id'] = $proid;
				$tmp ['category_tag_id'] = $value;
				$data [] = $tmp;
			}
			foreach ( $_REQUEST ['tag'] as $value ) {
				$tmp = array ();
				$tmp ['type'] = $_REQUEST ['tagtype'];
				$tmp ['product_id'] = $proid;
				$tmp ['category_tag_id'] = $value;
				$data [] = $tmp;
			}
			$tmp = array ();
			$tmp ['product_id'] = $proid;
			$tmp ['category_tag_id'] = $_REQUEST ['category_tag_id'];
			$tmp ['type'] = 1;
			$data [] = $tmp;
			$error = true;
			$retid = $this->protocate->addAll ( $data );
			if ($retid) {
				$_POST ['create_uid'] = $_POST ['write_uid'] = $_SESSION ['userid'];
				$_POST ['create_date'] = $_POST ['write_date'] = time ();
				$_POST ['id'] = $proid;
				$product = M ( 'Product' );
				echo self::upsave ( $product, $_POST );
			} else {
				$error = true;
			}
		}
		if ($error) {
			unset ( $_POST ['category_tag_id'] );
			$_POST ['create_uid'] = $_POST ['write_uid'] = $_SESSION ['userid'];
			$_POST ['create_date'] = $_POST ['write_date'] = time ();
			$_POST ['id'] = $proid;
			$product = M ( 'Product' );
			echo self::upsave ( $product, $_POST );
		} else {
			return $this->_ajm ( 300, '修改失败，请重试', 0 );
			exit ();
		}
		if ($retid) {
			unset ( $_POST ['category_tag_id'] );
			$_POST ['create_uid'] = $_POST ['write_uid'] = $_SESSION ['userid'];
			$_POST ['create_date'] = $_POST ['write_date'] = time ();
			$_POST ['id'] = $proid;
			$product = M ( 'Product' );
			echo self::upsave ( $product, $_POST );
		} else {
			return $this->_ajm ( 300, '修改失败，请重试', 0 );
			exit ();
		}
	}
	
	/**
	 * 产品描述信息
	 */
	function description() {
		$id = $this->prodes->field ( 'id' )->where ( array (
				'product_id' => $_REQUEST ['proid'] 
		) )->find ();
		$_POST ['id'] = $id ['id'];
		echo self::upsave ( $this->prodes, $_POST );
	}
	/**
	 * 把信息更新到数据库
	 * 
	 * @param object $_db        	
	 * @param array $_info        	
	 */
	function upsave($_db, $_info) {
		if ($_db->create ( $_info )) {
			// $id = $_db->save($_info);
			// echo $_db->getlastsql();
			if ($_db->save ( $_info )) {
				return $this->_ajm ( 200, '修改成功！', 0 );
			} else {
				return $this->_ajm ( 300, '修改失败，请重试', 0 );
			}
		} else {
			return $this->_ajm ( 300, '数据创建失败，请重试', 0 );
		}
	}
	/**
	 * 产品属性添加表单
	 */
	function attredit() {
		$cid = $_REQUEST ['cid'];
		$proid = $_REQUEST ['id'];
		$sql = 'SELECT a.id,name,alias,value FROM attribute as a LEFT JOIN category_attribute as c ON a.code=c.code WHERE c.category_id=' . $cid;
		$sql2 = 'SELECT name,alias FROM attribute as a LEFT JOIN category_attribute as c ON a.code=c.code WHERE c.category_id=' . $cid . ' GROUP BY name';
		$attr = M ()->query ( $sql );
		$attrarray = M ()->query ( $sql2 );
		$temp = array ();
		foreach ( $attrarray as $key => $val ) {
			$temp [$key] ['name'] = $val ['name'];
			$temp [$key] ['alias'] = $val ['alias'];
			foreach ( $attr as $k => $v ) {
				if ($val ['name'] == $v ['name']) {
					$temp [$key] ['value'] [$v ['id']] = $v ['value'];
					unset ( $attr [$k] );
				}
			}
		}
		$proattribute = $this->proattr->where ( array (
				'product_id' => $proid 
		) )->select ();
		$proattrids = array ();
		foreach ( $proattribute as $k => $v ) {
			$proattrids [] = $v ['attribute_id'];
		}
		$map ['id'] = array (
				'in',
				$proattrids 
		);
		$tempattr = $this->attribute->field ( 'id,name' )->where ( $map )->select ();
		$proatt = array ();
		foreach ( $tempattr as $v ) {
			$proatt [$v ['name']] = $v ['id'];
		}
		$proattrids = implode ( ',', $proattrids );
		$this->assign ( 'proattrids', $proattrids );
		$this->assign ( 'proattr', $proatt );
		$this->assign ( 'proid', $proid );
		$this->assign ( 'attrarray', $temp );
		$this->display ();
	}
	
	/**
	 * 产品属性修改、更新
	 */
	function upattr() {
		$proattrids = explode ( ',', $_REQUEST ['proattrids'] );
		$proid = $_REQUEST ['proid'];
		unset ( $_REQUEST ['proattrids'] );
		unset ( $_REQUEST ['proid'] );
		unset ( $_REQUEST ['_URL_'] );
		$i = 0;
		$fields = array ();
		foreach ( $_REQUEST as $k => $v ) {
			$fields [$i] ['id'] = $proattrids [$i];
			$fields [$i] ['attribute_id'] = $v;
			$fields [$i] ['product_id'] = $proid;
			$i ++;
		}
		$reid = $this->proattr->addall ( $fields, array (), true );
		if ($reid) {
			$this->_ajm ( '200', '添加成功！' );
		} else {
			$this->_ajm ( '300', '添加失败！' );
		}
	}
	
	/**
	 * 产品属性添加写入数据库
	 * (non-PHPdoc)
	 * 
	 * @see BaseAction::insert()
	 */
	function insert() {
		$proid = $_REQUEST ['proid'];
		unset ( $_REQUEST ['proid'] );
		unset ( $_REQUEST ['_URL_'] );
		$temp = array ();
		foreach ( $_REQUEST as $k => $v ) {
			$temp [] = array (
					'product_id' => $proid,
					'attribute_id' => $v 
			);
		}
		$reid = $this->proattr->addall ( $temp );
		if ($reid) {
			$this->_ajm ( '200', '添加成功！' );
		} else {
			$this->_ajm ( '300', '添加失败！' );
		}
	}
	/**
	 * 产品图片上传表单处理
	 */
	function do_photo() {
		$filePath = $this->_upload ( array (
				'savePath' => './Images/' . date ( 'Y-m' ) . '/' 
		) );
		
		$img_m = M ( 'product_image' );
		$img_arr = array (
				'product_id' => $_REQUEST ['id'],
				'create_uid' => $_REQUEST ['uid'],
				'create_date' => time (),
				'url' => $filePath 
		);
		$img_m->add ( $img_arr );
	}
	
	/**
	 * 产品图片上传表单处理
	 */
	function photo() {
		$this->assign ( 'session_id', session_id () );
		$this->display ();
	}
	function eidt() {
		$this->display ( 'index_edit' );
	}
	function price() {
		$pids = $this->dao->getIdsByCategoryId ( intval ( $_REQUEST ['cid'] ) );
		$map ['id'] = array (
				'in',
				$pids 
		);
		parent::dwz_list ( $map, $this->dao, 'index_price' );
	}
	function update_price() {
		$data = array ();
		$product_m = M ( 'Product' );
		if (is_array ( $_POST ['id'] )) {
			for($i = 0, $num = count ( $_POST ['id'] ); $i < $num; $i ++) {
				$data ['id'] = $_POST ['id'] [$i];
				$data ['price'] = $_POST ['price'] [$i];
				$product_m->save ( $data );
			}
		} else {
			$data ['id'] = $_POST ['id'];
			$data ['price'] = $_POST ['price'];
			$product_m->save ( $data );
		}
	}
	
	/**
	 * 添加产品价格
	 */
	function add_price() {
		var_dump ( $_REQUEST );
		$dao = M ( 'product_price_list' );
		$dao->add ( $_REQUEST );
	}
	
	/**
	 */
	function index_price_page() {
		$map ['id'] = $_GET ['id'];
		$model = D ( 'ProductPriceListView' );
		parent::dwz_list ( $map, $model );
	}
	
	/**
	 * 产品录入
	 */
	function entry() {
		$this->display ();
	}
	
	/**
	 * 产品录入表单处理
	 */
	function do_entry() {
		if ($_POST ['code'] && $_POST ['cost_price']) {
			$re = array (
					'type' => '1',
					'msg' => 'Success' 
			);
			
			$relation_list = array (
					'category',
					'brand',
					'suppliers' 
			);
			foreach ( $relation_list as $name ) {
				$_POST [$name . '_id'] = array_values ( $_POST [$name] );
				unset ( $_POST [$name] );
			}
			
			$data_array = array ();
			foreach ( $_POST as $key => $field ) {
				for($i = 0, $num = count ( $field ); $i < $num; $i ++) {
					$data_array [$i] [$key] = $field [$i];
				}
			}
			foreach ( $data_array as $key => $value ) {
				if (! $value ['code'] || ! $value ['cost_price']) { // code, cost_price为必填 项
					unset ( $data_array [$key] );
				}
			}
			
			$dao = M ( 'product_register' );
			$re_num = $dao->addAll ( $data_array );
			if (! $re_num) {
				$re = array (
						'type' => '2',
						'msg' => 'Error input' 
				);
			}
		} else {
			$re = array (
					'type' => '3',
					'msg' => 'Error url' 
			);
		}
		
		if ($num > 1 && $re_num)
			$this->_ajm ( 200, '产品登记成功！', 1, U ( 'Product/entry' ) );
		else
			echo (json_encode ( $re ));
	}
	function _after_do_entry() {
		import ( "@.Com.ProductLog" );
		ProductLog::save ( $_POST, 'entry' );
	}
	function _after_do_verify() {
		if (is_array ( $_REQUEST ['id'] )) {
			foreach ( $_REQUEST ['id'] as $value ) {
				$post [] = array (
						'id' => $value,
						'satus' => $_REQUEST ['satus'] 
				);
			}
		} else
			$post = array (
					'id' => $_REQUEST ['id'],
					'satus' => $_REQUEST ['satus'] 
			);
		;
		import ( "@.Com.ProductLog" );
		ProductLog::save ( $post, 'verify' );
	}
	
	/**
	 * 产品信息审核
	 */
	function verify() {
		$pids = $this->dao->getIdsByCategoryId ( intval ( $_REQUEST ['cid'] ) );
		
		$map ['status'] = '0';
		$map ['id'] = array (
				'in',
				$pids 
		);
		parent::dwz_list ( $map, $this->dao );
	}
	
	/**
	 * 产品信息审核处理
	 */
	function do_verify() {
		if ($_REQUEST ['status'] == 2) {
			$is_full = 0;
			if (is_array ( $_REQUEST ['id'] )) {
				// 批量审核
				$ids = array ();
				foreach ( $_REQUEST ['id'] as $id ) {
					$obj = $this->dao->field ( array (
							'price',
							'code' 
					) )->find ( intval ( $id ) );
					if (! $obj ['price'] && ! $obj ['code']) {
						$is_full = 1;
						break;
					}
					$ids [] = intval ( $id );
				}
				if ($ids) {
					$ids = implode ( ',', $ids );
				}
				if (! $is_full)
					$this->dao->where ( "id IN ({$ids})" )->save ( array (
							'status' => 2 
					) );
			} else {
				// 单个审核
				$obj = $this->dao->field ( array (
						'price',
						'code' 
				) )->find ( intval ( $_REQUEST ['id'] ) );
				if ($obj ['price'] && $obj ['code'])
					$this->dao->where ( "id=" . intval ( $_REQUEST ['id'] ) )->save ( array (
							'status' => 2 
					) );
				else
					$is_full = 1;
			}
			
			if (! $is_full)
				$this->_ajm ( 200, "审核成功！", 1, U ( 'Product/verify', array (
						'cid' => $_REQUEST ['cid'] 
				) ) );
			else
				$this->_ajm ( "300", "信息未完整" );
		}
		// 更多的其他更新操作
		
		$this->_ajm ( "300", "未找到操作目标！" );
	}
	
	/**
	 * 用EXCEL表录入产品
	 *
	 * 上传EXCEL文件，并导入数据到数据库
	 * 这里默认的是规范格式EXCEL文件，记录数据时，会去除头部（三行）和尾部（三行）
	 */
	function do_upload_excel() {
		if ($_FILES ['product_xls'] ['size'] && ($filePath = $this->_upload ())) {
			import ( "@.Com.PHPExcel", '', '.php' );
			Read_Excel_File ( $filePath, $temp );
			$temp = array_pop ( $temp );
			$dao = M ( 'product_register' );
			$dao_c = M ( 'product_category_tag' );
			$dao_s = M ( 'product_suppliers' );
			$dao_b = M ( 'product_brand' );
			$dao->add ();
			$data = array ();
			for($i = 3, $num = count ( $temp ) - 3; $i < $num; $i ++) {
				if (! $temp [$i] [3])
					continue;
				$product_c = $dao_c->field ( 'id' )->where ( array (
						'name' => $temp [$i] [1] 
				) )->find ();
				$product_b = $dao_b->field ( 'id' )->where ( array (
						'name' => $temp [$i] [2] 
				) )->find ();
				$product_s = $dao_s->field ( 'id' )->where ( array (
						'name' => $temp [$i] [9] 
				) )->find ();
				
				$product_array = array ();
				$product_array ['category_id'] = $product_c ['id'] ? $product_c ['id'] : 0;
				$product_array ['brand_id'] = $product_b ['id'] ? $product_b ['id'] : 0;
				$product_array ['code'] = $temp [$i] [3];
				$product_array ['name'] = $temp [$i] [4];
				$product_array ['cost_price'] = $temp [$i] [5];
				$product_array ['color'] = $temp [$i] [6];
				$product_array ['qty'] = $temp [$i] [7];
				$product_array ['material'] = $temp [$i] [8];
				$product_array ['suppliers_id'] = $product_s ['id'] ? $product_s ['id'] : 0;
				$product_array ['create_id'] = '12717'; // DOTO
				$product_array ['create_date'] = time ();
				$clolor_arr = explode ( '|', $product_array ['color'] );
				foreach ( $clolor_arr as $value ) {
					$product_array ['color'] = $value;
					$data [] = $product_array;
				}
			}
			if ($dao->addAll ( $data ))
				$this->_ajm ( "200", count ( $data ) . "个产品添加成功", 1, U ( GROUP_NAME . '/' . MODULE_NAME . '/entry' ) );
			else
				$this->_ajm ( "300", "数据插入失败" );
		} else {
			$this->_ajm ( "300", "文件上传失败" );
		}
	}
	
	function mname(){
		$ids = $_REQUEST ['ids'];
		$ftodo = $_REQUEST ['ftodo'];
		if ( $ftodo == 'view' ) {
			echo json_encode(array('action'=>'upmanme','ids'=>implode(',',$_REQUEST['ids']),'ftodo'=>'edit'));die();
		} elseif ( $ftodo == 'edit' ) {
			$this->assign('ids',$_REQUEST['ids']);
			$this->display();
		} elseif ( $ftodo == 'insert' ) {
			$mn = $_REQUEST ['mn'];
			$re = M('product')->where(array('id'=>array('in',$ids)))->save(array('mname'=>$mn));
			if ($re || $re===0 ) $this->_ajm ( "200", "修改成功", 1 );
			else $this->_ajm ( "300", "修改失败".M('product')->getLastSql() );
		}
	}
}
function jsonString($str) {
	return preg_replace ( "/([\\\\\/'])/", '\\\$1', $str );
}

